import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 涛
 * Date: 2023-10-06
 * Time: 9:10
 */
public class BinaryTree {
    static class TreeNode {
        public char val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }

    public TreeNode createTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');

        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;

        return A;
    }
    //前序遍历 根左右
    void preOrder(TreeNode root) {
        //判断传入的树是不是空,是空就直接走人
        if(root == null) {
            return;
        }
        System.out.print(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);
    }
    //中序遍历 左根右
    void inOrder(TreeNode root) {
        if(root == null) {
            return;
        }
        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);
    }
    void inOrder2(TreeNode root) {
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        while(cur != null || !stack.isEmpty()) {
            while(cur != null) {
                stack.push(cur);
                cur = cur.left;
            }
            TreeNode top = stack.pop();
            System.out.println(top.val + " ");
            cur = top.right;
        }
    }
    //后续遍历 左右根
    void postOrder(TreeNode root) {
        if(root == null) {
            return;
        }
        postOrder(root.left);
        postOrder((root.right));
        System.out.print(root.val + " ");
    }

    // 获取树中节点的个数
    int size(TreeNode root) {
        //子问题的思路
        if(root == null) {
            return 0;
        }
        return size(root.left) + size(root.right) + 1;
    }
    // 获取树中节点的个数
    int leafSize1 = 0;
    int size1(TreeNode root) {
        //遍历思路
        if(root == null) {
            return 0;
        }
        size1(root.left);
        size1(root.right);
        leafSize1++;
        return leafSize1;
    }

    //获取叶子节点的个数
    //叶子节点的计数器
    //遍历思路
    public int leafSize  = 0;
    int getLeafNodeCount1(TreeNode root) {
        if(root == null) {
            return 0;
        }
        if(root.left == null && root.right == null) {
            leafSize++;
        }
        getLeafNodeCount1(root.left);
        getLeafNodeCount1(root.right);
        return leafSize;
    }
    //子问题思路,求叶子节点
    int getLeafNodeCount(TreeNode root) {
        if(root == null) {
            return 0;
        }
        if(root.left == null && root.right == null) {
            return 1;
        }
        return getLeafNodeCount(root.left) + getLeafNodeCount(root.right);
    }
    // 获取第K层节点的个数
    int getKLevelNodeCount(TreeNode root,int k) {
       if(root == null) {
           return 0;
       }
        if(k == 1) {
            return 1;
        }
        return getKLevelNodeCount(root.left, k - 1) + getKLevelNodeCount(root.right, k - 1);
    }
    // 获取二叉树的高度
    int getHeight(TreeNode root) {
        if(root == null) {
            return 0;
        }

        int leftHight = getHeight(root.left);
        int rightHight = getHeight(root.right);
        return leftHight > rightHight ? leftHight + 1 : rightHight + 1;
        //也可以直接将左右高度放到return那里,但是重复多计算一次return那里的递归，力扣题跑不过
    }

    // 检测值为value的元素是否存在
    TreeNode find(TreeNode root, char val1) {
        if(root == null) {
            return null;
        }
        if(root.val == val1) {
            return root;
        }
        TreeNode ret1 = find(root.left, val1);
        if(ret1 != null) {
            return ret1;
        }
        TreeNode ret2 = find(root.right, val1);
        if (ret2 != null) {
            return ret2;
        }
        return null;
    }
}
